package com.example.mylistview;

import java.util.List;

import com.example.mylistview.expandable.ExpandableLinearLayout;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

/**
 * @Path com.example.mylistview.MyBaseAdapter.java
 * @Author 严旭文
 * @Date 2016-12-8 上午10:49:06
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class MyBaseAdapter extends BaseAdapter {
	private List<?> list;
	private Context context;
	// 下面为展开列表所用到的
	private int expand_layout;

	public MyBaseAdapter(Context context, List<?> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		return convertView;
	}

	public class BaseViewHolder implements  View.OnFocusChangeListener{
		public ExpandableLinearLayout expandableLayout;
		public View expand;

		 /**
	     *设置控件展开按钮
	     */
	    public void setExpandView(View v){
	        expand=v;
	        expand.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	                if(expand!=null) {
	                    if (expand.isFocused()) {
	                        expand.clearFocus();
	                    } else {
	                        expand.requestFocus();
	                    }
	                }
	            }
	        });
	        expand.setFocusableInTouchMode(true);
	        expand.setOnFocusChangeListener(this);
	    }

		@Override
	    public void onFocusChange(View v, boolean hasFocus) {
            if (expandableLayout != null) {
                if (hasFocus) {
//                    expand_holder=this;
                    expandableLayout.expand();
                } else {
                    expandableLayout.collapse();
                }
            }
        }
	}
	private View convertView;
	public View getConvertView(){
		return convertView;
	}
	
    public void setViewHolder(View convertView,BaseViewHolder viewholder){
    	this.convertView=convertView;
    	convertView.setTag(viewholder);
    }
    public BaseViewHolder getViewHolder(){
    	return (BaseViewHolder) convertView.getTag();
    }

	public void addExpand(int expand_layout) {
		this.expand_layout = expand_layout;
	}
}
